package com.dinzeer.lostbladereborn.se;


import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.legendreliclib.lib.util.text.AdvancedGradientTextHelper;
import com.dinzeer.legendreliclib.lib.util.text.ColorPreset;
import com.dinzeer.lostbladereborn.regsitry.LBRSpecialEffectRegsitry;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraftforge.event.ItemAttributeModifierEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@Mod.EventBusSubscriber
public class StarsSEEX extends SpecialEffectEx {

    public StarsSEEX() {
        super(60);
    }
    @SubscribeEvent
    public static void DubheEX2(ItemAttributeModifierEvent s){
        if (s.getSlotType() != EquipmentSlot.MAINHAND)return;
        if (hasSpecialEffect(s.getItemStack(), LBRSpecialEffectRegsitry.DubheEX.getId()) ){
            int Refine= SlashBladeUtil.getRefine(s.getItemStack());
            double coefficient=Refine/10;
            if (coefficient>40){
                coefficient = 40;
            }
            s.addModifier(Attributes.ATTACK_DAMAGE,new AttributeModifier(UUID.nameUUIDFromBytes("tianshu2".getBytes()),"tianshu2",
                   coefficient, AttributeModifier.Operation.ADDITION));
        }
    }
    @SubscribeEvent
    public static void HagenEX2(ItemAttributeModifierEvent s){
        if (hasSpecialEffect(s.getItemStack(), LBRSpecialEffectRegsitry.HagenEX.getId()) ){
            if (s.getSlotType() != EquipmentSlot.MAINHAND)return;

            int Refine= SlashBladeUtil.getRefine(s.getItemStack());
            double coefficient=Refine/10;
            if (coefficient>40){
                coefficient = 40;
            }
            s.addModifier(Attributes.ARMOR_TOUGHNESS,new AttributeModifier(UUID.nameUUIDFromBytes("tianxuan2".getBytes()),"tianxuan2",
                    coefficient, AttributeModifier.Operation.ADDITION));
        }
    }
    @SubscribeEvent
    public static void Phecda2(LivingHurtEvent event){
        hurtEventHandleSe(event,(p,l,state)->{

            int mobLooting = p.getMainHandItem().getEnchantmentLevel(Enchantments.MOB_LOOTING);
            hurtEventAmountBase(event, Math.min(mobLooting *0.05f,1.25f));
        },LBRSpecialEffectRegsitry.PhecdaEX.getId());
    }
    @SubscribeEvent
    public static void Megrez2(LivingHurtEvent event){
        hurtEventHandleSe(event,(p,l,state)->{
            hurtEventAmountBase(event, Math.min(((int) (p.experienceLevel/30))*0.05f,1.25f));

            },LBRSpecialEffectRegsitry.MegrezEX.getId());
    }
    @SubscribeEvent
    public static void Fenrir2(LivingHurtEvent event){
        hurtEventHandleSe(event,(p,l,state)->{
            hurtEventAmountBase(event, Math.min(((int) (state.getProudSoulCount()/2000))*0.05f,1.25f));
        },LBRSpecialEffectRegsitry.FenrirEX.getId());
    }
    @SubscribeEvent
    public static void Mizar2(LivingHurtEvent event){
        hurtEventHandleSe(event,(p,l,state)->{
            Map<Enchantment, Integer> allEnchantments = p.getMainHandItem().getAllEnchantments();
            AtomicInteger value = new AtomicInteger();
            allEnchantments.forEach(
                    (k,v)->{
                        value.addAndGet(v);
                    }
            );
            hurtEventAmountBase(event, (float) Math.max(0.5-value.get()*0.1f,1.25f));
        },LBRSpecialEffectRegsitry.MizarEX.getId());
    }
    @SubscribeEvent
    public static void Alkaid2(LivingHurtEvent event){
        if (event.getEntity() instanceof Player player){
            if (hasSpecialEffect2(player.getMainHandItem(),LBRSpecialEffectRegsitry.AlkaidEX.getId(), player)){
                hurtEventAmountBase(event, (float) Math.max(-0.1,-0.01*player.getAttributeValue(Attributes.LUCK)));
            }
        }
    }

    @Override
    public Component getDescription() {
        return AdvancedGradientTextHelper.createGradientText(super.getDescription().getString(),
                ColorPreset.RAINBOW,
                AdvancedGradientTextHelper.GradientMode.PULSE);
    }
}
